package com.gutsyzhan.bilibili.controller;

import com.gutsyzhan.bilibili.domain.*;
import com.gutsyzhan.bilibili.service.VideoService;
import com.gutsyzhan.bilibili.support.UserSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
public class VideoController {
    private static final Logger logger = LoggerFactory.getLogger(VideoController.class);
    @Resource
    private VideoService videoService;

    @Resource
    private UserSupport userSupport;

    /**
     * 视频投稿
     */
    @PostMapping("/videos")
    public JsonResponse<String> addVideo(@RequestBody Video video){
        Long userId = userSupport.getCurrentUserId();
        video.setUserId(userId);
        videoService.addVideo(video);
        return JsonResponse.success();
    }

    /**
     * 瀑布流视频列表
     * @param no 第几页
     * @param size 每页展示个数
     * @param area 分区
     * @return
     */
    @GetMapping("/videos")
    public JsonResponse<PageResult<Video>> pageListVideos(Integer no, Integer size, String area){
        PageResult<Video> result = videoService.pageListVideos(no, size, area);
        return new JsonResponse<>(result);
    }

    /**
     * 在线播放视频
     * @param request 请求
     * @param response 响应
     * @param path 视频文件相对路径
     * @throws Exception
     */
    @GetMapping("/video-slices")
    public void viewVideoOnlineBySlices(HttpServletRequest request, HttpServletResponse response, String path) throws Exception {
        videoService.viewVideoOnlineBySlices(request, response, path);
    }


    /**
     * 点赞视频
     * @param videoId 视频id
     */
    @PostMapping("/video-likes")
    public JsonResponse<String> addVideoLike(@RequestParam("videoId")Long videoId) {
        Long userId = userSupport.getCurrentUserId();
        videoService.addVideoLike(userId, videoId);
        return JsonResponse.success();
    }

    /**
     * 取消点赞视频
     * @param videoId 视频id
     */
    @DeleteMapping("/video-likes")
    public JsonResponse<String> deleteVideoLike(@RequestParam("videoId")Long videoId) {
        Long userId = userSupport.getCurrentUserId();
        videoService.deleteVideoLike(userId, videoId);
        return JsonResponse.success();
    }

    /**
     * 查询某一视频的点赞数量
     * 注意，用户不登录也是可以查询到某一视频的点赞数量
     * 如果当前用户已登录，那么需要返回当前用户对该视频是否已点赞
     * @param videoId 视频id
     */
    @GetMapping("/video-likes")
    public JsonResponse<Map<String, Object>> getVideoLikes(@RequestParam("videoId")Long videoId) {
        Long userId = null;
        try{
            userId = userSupport.getCurrentUserId();
        }catch (Exception e){
            logger.info("获取用户信息失败，异常信息为:{}", e.getMessage());
        }
        Map<String, Object> result = videoService.getVideoLikes(userId, videoId);
        return new JsonResponse<>(result);
    }

    /**
     * 收藏视频
     */
    @PostMapping("/video-collections")
    public JsonResponse<String> addVideoCollection(@RequestBody VideoCollection videoCollection) {
        Long userId = userSupport.getCurrentUserId();
        videoService.addVideoCollection(userId, videoCollection);
        return JsonResponse.success();
    }

    /**
     * 取消收藏视频
     * @param videoId 视频id
     */
    @DeleteMapping("/video-collections")
    public JsonResponse<String> deleteVideoCollection(@RequestParam("videoId")Long videoId) {
        Long userId = userSupport.getCurrentUserId();
        videoService.deleteVideoCollection(userId, videoId);
        return JsonResponse.success();
    }

    /**
     * 查询某一视频的收藏数量
     * 注意，用户不登录也是可以查询到某一视频的收藏数量
     * 如果当前用户已登录，那么需要返回当前用户对该视频是否已收藏
     * @param videoId 视频id
     */
    @GetMapping("/video-collections")
    public JsonResponse<Map<String, Object>> getVideoCollections(@RequestParam("videoId")Long videoId) {
        Long userId = null;
        try{
            userId = userSupport.getCurrentUserId();
        }catch (Exception e){
            logger.info("获取用户信息失败，异常信息为:{}", e.getMessage());
        }
        Map<String, Object> result = videoService.getVideoCollections(userId, videoId);
        return new JsonResponse<>(result);
    }

    /**
     * 视频投币
     */
    @PostMapping("/video-coins")
    public JsonResponse<String> addVideoCoin(@RequestBody VideoCoin videoCoin) {
        Long userId = userSupport.getCurrentUserId();
        videoService.addVideoCoin(userId, videoCoin);
        return JsonResponse.success();
    }

    /**
     * 查询某一视频的投币数量
     * 注意，用户不登录也是可以查询到某一视频的投币数量
     * 如果当前用户已登录，那么需要返回当前用户对该视频是否已投币
     * @param videoId 视频id
     */
    @GetMapping("/video-coins")
    public JsonResponse<Map<String, Object>> getVideoCoins(@RequestParam("videoId")Long videoId) {
        Long userId = null;
        try{
            userId = userSupport.getCurrentUserId();
        }catch (Exception e){
            logger.info("获取用户信息失败，异常信息为:{}", e.getMessage());
        }
        Map<String, Object> result = videoService.getVideoCoins(userId, videoId);
        return new JsonResponse<>(result);
    }

    /**
     * 视频评论
     */
    @PostMapping("/video-comments")
    public JsonResponse<String> addVideoComment(@RequestBody VideoComment videoComment) {
        Long userId = userSupport.getCurrentUserId();
        videoService.addVideoComment(userId, videoComment);
        return JsonResponse.success();
    }

    /**
     * 分页查询视频评论
     * @param videoId 视频id
     */
    @GetMapping("/video-comments")
    public JsonResponse<PageResult<VideoComment>> pageListVideoComments(@RequestParam("videoId")Long videoId,
                                                                        @RequestParam("no")Integer no,
                                                                        @RequestParam("size")Integer size) {
        PageResult<VideoComment> result = videoService.pageListVideoComments(videoId ,no, size);
        return new JsonResponse<>(result);
    }

    /**
     * 获取视频详情
     * @param videoId 视频id
     */
    @GetMapping("/video-details")
    public JsonResponse<Map<String, Object>> getVideoDetails(@RequestParam("videoId")Long videoId) {
        Map<String, Object> result = videoService.getVideoDetails(videoId);
        return new JsonResponse<>(result);
    }
}
